package de.carcophan.archiver.util;

import org.apache.commons.compress.archivers.sevenz.SevenZOutputFile;
import org.apache.commons.io.DirectoryWalker;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

/**
 * Created by  on 16.04.2014.
 */
public class FileWalker extends DirectoryWalker {

  private static final Logger logger = LogManager.getLogger(FileWalker.class);

  private FileWalkerHandler handler;

  public void doWalk(File startDir, FileWalkerHandler handler) {
    logger.debug("doWalk() startDir = " + startDir.getAbsolutePath());
    this.handler = handler;
    try {
      ArrayList results = new ArrayList();
      walk(startDir, results);
    } catch (IOException e) {
      logger.error("error in Filewalker.", e);
      throw new RuntimeException(e);
    }
  }

  @Override
  protected void handleFile(File file, int depth, Collection results) throws IOException {
    logger.trace("handleFile() - " + file.getAbsolutePath() + " depth=" + depth, results);
    handler.handle(file);
  }
}
